home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / Direct3D / AnimKeys / AnimKeys.frm (.txt) next >
Encoding:
Visual Basic Form  |  2001-10-08  |  8.0 KB  |  221 lines

  1. VERSION 5.00
  2. Begin VB.Form Form3 
  3.    Caption         =   "Animate Key Frames"
  4.    ClientHeight    =   6015
  5.    ClientLeft      =   60
  6.    ClientTop       =   345
  7.    ClientWidth     =   7530
  8.    Icon            =   "AnimKeys.frx":0000
  9.    LinkTopic       =   "Form3"
  10.    ScaleHeight     =   401
  11.    ScaleMode       =   3  'Pixel
  12.    ScaleWidth      =   502
  13.    StartUpPosition =   3  'Windows Default
  14. Attribute VB_Name = "Form3"
  15. Attribute VB_GlobalNameSpace = False
  16. Attribute VB_Creatable = False
  17. Attribute VB_PredeclaredId = True
  18. Attribute VB_Exposed = False
  19. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  20. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  21. '  File:       AnimKeys.frm
  22. '  Content:    Playback of animated geometry
  23. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  24. Option Explicit
  25. Dim Character As CD3DFrame
  26. Dim Animation As CD3DAnimation
  27. Dim MediaDir As String
  28. Dim m_bInit As Boolean
  29. Dim m_bMinimized As Boolean
  30. '-----------------------------------------------------------------------------
  31. ' Name: Form_Load()
  32. ' Desc: Main entry point for the sample
  33. '-----------------------------------------------------------------------------
  34. Private Sub Form_Load()
  35.     Dim hr As Long
  36.     ' Show the form
  37.     Me.Show
  38.     DoEvents
  39.     ' Initialize D3D
  40.     ' Note: D3DUtil_Init will attempt to use D3D Hardware acceleartion.
  41.     ' If it is not available it attempt to use the Software Reference Rasterizer.
  42.     ' If all fail it will display a message box indicating so.
  43.     '
  44.     m_bInit = D3DUtil_Init(Me.hwnd, True, 0, 0, D3DDEVTYPE_HAL, Nothing)
  45.     If Not (m_bInit) Then End
  46.                    
  47.     ' Find a path to our media
  48.     MediaDir = FindMediaDir("skmech.x")
  49.     D3DUtil_SetMediaPath MediaDir
  50.     ' Load Character and Animation Data
  51.     InitDeviceObjects
  52.     ' Position camera and Lights
  53.     RestoreDeviceObjects
  54.     ' Start our timer
  55.     DXUtil_Timer TIMER_start
  56.     ' Loop forever rendering our animation
  57.     Do While True
  58.         'Have our animation pose our character
  59.         Animation.SetTime DXUtil_Timer(TIMER_GETAPPTIME) * 30
  60.         
  61.         'See what state the device is in.
  62.         hr = g_dev.TestCooperativeLevel
  63.         If hr = D3DERR_DEVICENOTRESET Then
  64.             g_dev.Reset g_d3dpp
  65.             RestoreDeviceObjects
  66.         End If
  67.         
  68.         'dont bother rendering if we are not ready yet
  69.         If hr = 0 Then
  70.             
  71.             'Clear the background to ARGB grey
  72.             D3DUtil_ClearAll &HFF909090
  73.         
  74.             'Start the Scene
  75.             g_dev.BeginScene
  76.             
  77.             'Render the character (g_dev defined in D3DUtil)
  78.             Character.Render g_dev
  79.             
  80.             'End the scene
  81.             g_dev.EndScene
  82.             
  83.             'Update the Scene to our window
  84.             D3DUtil_PresentAll Me.hwnd
  85.             
  86.         End If
  87.         
  88.         'Allow VB events to process
  89.         DoEvents
  90.         
  91.     Loop
  92. End Sub
  93. '-----------------------------------------------------------------------------
  94. ' Name: InitDeviceObjects()
  95. ' Desc: Load Character and Animation Data
  96. '-----------------------------------------------------------------------------
  97. Sub InitDeviceObjects()
  98.     'Create an Animation object to hold any animations
  99.     Set Animation = New CD3DAnimation
  100.     'Create a Frame object from a file
  101.     'the Animation object will parent any animations in the file
  102.     Set Character = D3DUtil_LoadFromFile(MediaDir + "skmech.x", Nothing, Animation)
  103. End Sub
  104. '-----------------------------------------------------------------------------
  105. ' Name: InvalidateDeviceObjects()
  106. ' Desc: place code to release references to non-managed objects here
  107. '-----------------------------------------------------------------------------
  108. Sub InvalidateDeviceObjects()
  109.     'all objects are managed in this sample
  110. End Sub
  111. '-----------------------------------------------------------------------------
  112. ' Name: RestoreDeviceObjects()
  113. ' Desc: setup device state such as camera and light placement
  114. '-----------------------------------------------------------------------------
  115. Sub RestoreDeviceObjects()
  116.     ' Set up some lights and camera
  117.     g_lWindowWidth = Me.ScaleWidth
  118.     g_lWindowHeight = Me.ScaleHeight
  119.     D3DUtil_SetupDefaultScene
  120.     ' position the camera
  121.     D3DUtil_SetupCamera vec3(0, 0, 300), vec3(0, 0, 0), vec3(0, 1, 0)
  122. End Sub
  123. '-----------------------------------------------------------------------------
  124. ' Name: DeleteDeviceObjects()
  125. ' Desc: Called when the app is exitting, or the device is being changed,
  126. '       this function deletes any device dependant objects.
  127. '-----------------------------------------------------------------------------
  128. Public Sub DeleteDeviceObjects()
  129.     Set Character = Nothing
  130.     Set Animation = Nothing
  131.     m_bInit = False
  132. End Sub
  133. '-----------------------------------------------------------------------------
  134. ' Name: Form_KeyDown()
  135. ' Desc: Process key messages for exit and change device
  136. '-----------------------------------------------------------------------------
  137. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  138.      Select Case KeyCode
  139.         
  140.         Case vbKeyEscape
  141.             Unload Me
  142.             
  143.         Case vbKeyF2
  144.                 
  145.             ' Pause the timer
  146.             DXUtil_Timer TIMER_STOP
  147.             
  148.             ' Bring up the device selection dialog
  149.             ' we pass in the form so the selection process
  150.             ' can make calls into InitDeviceObjects
  151.             ' and RestoreDeviceObjects
  152.             frmSelectDevice.SelectDevice Me
  153.             
  154.             ' Restart the timer
  155.             DXUtil_Timer TIMER_start
  156.             
  157.         Case vbKeyReturn
  158.         
  159.             ' Check for Alt-Enter if not pressed exit
  160.             If Shift <> 4 Then Exit Sub
  161.             
  162.             ' If we are windowed go fullscreen
  163.             ' If we are fullscreen returned to windowed
  164.             If g_d3dpp.Windowed Then
  165.                  D3DUtil_ResetFullscreen
  166.             Else
  167.                  D3DUtil_ResetWindowed
  168.             End If
  169.                              
  170.             ' Call Restore after ever mode change
  171.             ' because calling reset looses state that needs to
  172.             ' be reinitialized
  173.             RestoreDeviceObjects
  174.            
  175.     End Select
  176. End Sub
  177. '-----------------------------------------------------------------------------
  178. ' Name: Form_Resize()
  179. ' Desc: hadle resizing of the D3D backbuffer
  180. '-----------------------------------------------------------------------------
  181. Private Sub Form_Resize()
  182.     ' If D3D is not initialized then exit
  183.     If Not m_bInit Then Exit Sub
  184.     ' If we are in a minimized state stop the timer and exit
  185.     If Me.WindowState = vbMinimized Then
  186.         DXUtil_Timer TIMER_STOP
  187.         m_bMinimized = True
  188.         Exit Sub
  189.         
  190.     ' If we just went from a minimized state to maximized
  191.     ' restart the timer
  192.     Else
  193.         If m_bMinimized = True Then
  194.             DXUtil_Timer TIMER_start
  195.             m_bMinimized = False
  196.         End If
  197.     End If
  198.         
  199.     ' Dont let the window get too small
  200.     If Me.ScaleWidth < 10 Then
  201.         Me.width = Screen.TwipsPerPixelX * 10
  202.         Exit Sub
  203.     End If
  204.     If Me.ScaleHeight < 10 Then
  205.         Me.height = Screen.TwipsPerPixelY * 10
  206.         Exit Sub
  207.     End If
  208.     'reset and resize our D3D backbuffer to the size of the window
  209.     D3DUtil_ResizeWindowed Me.hwnd
  210.     'All state get losts after a reset so we need to reinitialze it here
  211.     RestoreDeviceObjects
  212. End Sub
  213. '-----------------------------------------------------------------------------
  214. ' Name: Form_Unload()
  215. ' Desc:
  216. '-----------------------------------------------------------------------------
  217. Private Sub Form_Unload(Cancel As Integer)
  218.     DeleteDeviceObjects
  219.     End
  220. End Sub
  221.